﻿using Aspose.Cells;
using Coldairarrow.Business.Base_leasesystem_employee;
using Coldairarrow.Business.Base_leasesystem_merchant;
using Coldairarrow.Business.Base_leasesystem_transactionbatch;
using Coldairarrow.Entity.Base_leasesystem_transactionbatch;
using Coldairarrow.Entity.ViewModel;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using Coldairarrow.Util.Helper;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.Base_leasesystem_transactionbatch
{
    /// <summary>
    /// 代发主表
    /// </summary>
    [Route("/Base_leasesystem_transactionbatch/[controller]/[action]")]
    [OpenApiTag("代发主表")]
    public class leasesystem_transactionbatchController : BaseApiController
    {
        #region DI

        public leasesystem_transactionbatchController(
            Ileasesystem_transactionbatchBusiness leasesystem_transactionbatchBus,
            Ileasesystem_merchantBusiness leasesystem_merchantBusiness,
            Ileasesystem_employeeBusiness leasesystem_employeeBusiness,
            IOperator @operator
            )
        {
            _leasesystem_transactionbatchBus = leasesystem_transactionbatchBus;
            _leasesystem_merchantBusiness = leasesystem_merchantBusiness;
            _leasesystem_employeeBusiness = leasesystem_employeeBusiness;
            _operator = @operator;
        }

        Ileasesystem_transactionbatchBusiness _leasesystem_transactionbatchBus { get; }
        Ileasesystem_merchantBusiness _leasesystem_merchantBusiness { get; }
        readonly IOperator _operator;
        Ileasesystem_employeeBusiness _leasesystem_employeeBusiness { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<object> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _leasesystem_transactionbatchBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<leasesystem_transactionbatch> GetTheData(IdInputDTO input)
        {
            return await _leasesystem_transactionbatchBus.GetTheDataAsync(input.id);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(leasesystem_transactionbatch data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _leasesystem_transactionbatchBus.AddDataAsync(data);
            }
            else
            {
                await _leasesystem_transactionbatchBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        [ApiPermission("transactionbatch.Delete")]
        public async Task DeleteData(List<string> ids)
        {
            await _leasesystem_transactionbatchBus.DeleteDataAsync(ids);
        }

        [HttpPost]
        public async Task<object> ReadSettlement() 
        {
            var files = Request.Form.Files[0];
            var ms = new MemoryStream();
            files.CopyTo(ms);

            DataTable dt = new DataTable();
            Workbook book = new Workbook(ms);
            Worksheet sheet = book.Worksheets[0];
            Cells cells = sheet.Cells;
            

            dt = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
            return await  _leasesystem_transactionbatchBus.SettlementExcel(dt,_operator.UserId);
            
        }

        [HttpPost]
        public async Task AddListEntity([FromBody]List<DataTableHelp> list)
        {

           await _leasesystem_transactionbatchBus.AddListAsync(list, IdHelper.GetId(), _operator?.UserId);
        }
        #endregion
    }
}